package com.bdqn.day1231.service.impl;

import com.bdqn.day1231.entity.User;
import com.bdqn.day1231.mapper.UserMapper;
import com.bdqn.day1231.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 业务层代码
 */
@Service
public class UserServiceImpl implements UserService {
    //    注入数据库操作对象
    @Resource
    private UserMapper userMapper;

    @Override
    public String getUserByNameAndPass(String username, String password) {
        //此处返回的是记录中的id号,如果没有此用户将返回null
        Integer it = userMapper.selectUserByNameAndPass(username, password);
       // System.out.println("it是："+it);
//        if(it==null){
//            return "亲,用户名或密码不正确!";
//        }
        return it == null ? "亲,用户名或密码不正确!" : "ok";
    }

    @Override//注册判断用
    public boolean findUser(String username) {
        //查到返回对象,否则返回null
        Integer it = userMapper.selectUserByUsername(username);
        return it != null;
    }

    @Override//完成注册
    public String saveUser(User user) {
        //用户
        boolean flag = findUser(user.getUsername());
        if (!flag) {//用户不存在,可以注册flag==false
            userMapper.insertUser(user);
            return "亲,注册成功!";
        } else {
            return "亲,注册不正确!";
        }
    }
}
